package com.example.imoocmall.service.impl;

import com.example.imoocmall.common.Constant;
import com.example.imoocmall.exception.ImoocMallException;
import com.example.imoocmall.exception.ImoocMallExceptionEnum;
import com.example.imoocmall.model.dao.CartMapper;
import com.example.imoocmall.model.dao.ProductMapper;
import com.example.imoocmall.model.pojo.Cart;
import com.example.imoocmall.model.pojo.Product;
import com.example.imoocmall.service.CartService;
import com.example.imoocmall.model.vo.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UserService的实现类
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CartMapper cartMapper;

    @Override
    public List<CartVo> list(Integer userId){
        List<CartVo> cartVos = cartMapper.selectList(userId);
        for (CartVo cartVo : cartVos) {
            cartVo.setTotalPrice(cartVo.getQuantity()*cartVo.getPrice());
        }
        return cartVos;
    }

    @Override
    public List<CartVo> add(Integer userId, Integer productId, Integer count){
        validProduct(productId,count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId,productId);
        if (cart == null){
            //这个商品之前不再购物车里,需要新增
            cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(userId);
            cart.setQuantity(count);
            cart.setSelected(Constant.SaleSelect.CHECKED);
            cartMapper.insertSelective(cart);
        }else{
            //这个商品已经在购物车里面了,需要数量相加
            count = cart.getQuantity() + count;
            if (count>0){
                cart.setQuantity(count);
                cartMapper.updateByPrimaryKeySelective(cart);
            }else {
                this.delete(userId,productId);
            }

        }
        return list(userId);
    }
    private void validProduct(Integer productId, Integer count) {
        Product product = productMapper.selectByPrimaryKey(productId);
        //判断是否上架,是否存在
        if (product == null || product.getStatus().equals(Constant.SaleStatus.NOT_SALE)){
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE);
        }
        //判断库存
        if (count>product.getStock()){
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE_NUM);
        }
    }


    @Override
    public List<CartVo> update(Integer userId, Integer productId, Integer count){
        validProduct(productId,count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId,productId);
        if (cart == null){
//            //这个商品之前不再购物车里,需要新增
//            cart = new Cart();
//            cart.setProductId(productId);
//            cart.setUserId(userId);
//            cart.setQuantity(count);
//            cart.setSelected(Constant.SaleSelect.CHECKED);
//            cartMapper.insertSelective(cart);
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }else{
            //这个商品已经在购物车里面了,需要数量相加
            count = cart.getQuantity() + count;
            if (count>0){
                cart.setQuantity(count);
                cartMapper.updateByPrimaryKeySelective(cart);
            }else {
                this.delete(userId,productId);
            }
        }
        return list(userId);
    }


    @Override
    public List<CartVo> delete(Integer userId, Integer productId){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId,productId);
        if (cart == null){
//            //这个商品之前不再购物车里,需要新增
//            cart = new Cart();
//            cart.setProductId(productId);
//            cart.setUserId(userId);
//            cart.setQuantity(count);
//            cart.setSelected(Constant.SaleSelect.CHECKED);
//            cartMapper.insertSelective(cart);
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }else{
           cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return list(userId);
    }




    @Override
    public List<CartVo> selectOrNot(Integer userId, Integer productId, Integer selected){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId,productId);
        if (cart == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }else {
            cartMapper.selectOrNot(userId,productId,selected);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> selectOrNotAll(Integer userId,Integer selected){
        cartMapper.selectOrNot(userId,null,selected);
        return this.list(userId);
    }
}


















